package com.gk.carshopsys.service;

import com.gk.carshopsys.dao.EmpDao;
import com.gk.carshopsys.pojo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpDao empDao;


    @Override
    public int addEmpByList(List<Emp> list) {
        return empDao.addEmpByList(list);
    }

    @Override
    public Emp loginemp(String username, String password) {
        List<Emp> emp =empDao.findEmpByEmpno(username);

        if(emp.isEmpty()){
            return null;
        }

        Emp e=emp.get(0);
        if (e!=null){
            String passwd = e.getPassword();
            if(password.equals(passwd)){
                return e;
            }
        }
        return null;
    }

    @Override
    public List<Emp> findEmp(Emp emp) {
        return empDao.findEmp(emp);
    }

    @Override
    public int addEmp(Emp emp) {
       List<Emp> getemp =empDao.findEmpByEmpno(emp.getEmpno());
       if(getemp.isEmpty()){
           return empDao.addEmp(emp);
       }
       else {
           return -1;
       }

    }

    @Override
    public int delEmp(List<Integer> empList) {

        return empDao.delEmp(empList);
    }

    @Override
    public int updateEmp(Emp emp) {
        List<Emp> getemp =empDao.findEmpByEmpno(emp.getEmpno());
        if(getemp.size()>1){
            return -1;
        }
        else {
            return empDao.updateEmp(emp);
        }
    }

    @Override
    public int changePassword(Integer id, String password) {
        Emp emp = new Emp();
        emp.setId(id);
        emp.setPassword(password);
        return empDao.updatePasswordById(emp);
    }
}
